﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace GUI
{
    public partial class FormDatCho : Form
    {
        public FormDatCho()
        {
            InitializeComponent();
        }

        private void FormDatCho_Load(object sender, EventArgs e)        {
            
            this.txtGioDi.ReadOnly = true;
            this.txtGioDen.ReadOnly = true;
            this.txtBenXeDi.ReadOnly = true;
            this.txtBenXeDen.ReadOnly = true;
            List<KhachHangDTO> kh=KhachHangBUS.SelectKhachHangAll();
            foreach (var item in kh)
            {
                this.cbbMaKH.Items.Add(item.MAKH);
            }
            List<ChuyenXeDTO> mc = ChuyenXeBUS.SelectALL();
            foreach (var item in mc)
            {
                this.cbbMaChuyen.Items.Add(item.MACHUYEN);
                this.cbbBenXeDen.Items.Add(item.BENXEDEN);
            }
            this.dataGridView1.DataSource = DatChoBUS.SelectAllDatCho();
        }

        private void cbbMaChuyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            String mc = this.cbbMaChuyen.Text;
            LoadThongTinCX(mc);
        }
        void LoadThongTinCX(String machuyen)
        {
            this.cbbNgayDi.Items.Clear();
            ChuyenXeDTO cx = ChuyenXeBUS.SelectChuyenXeByMaChuyen(machuyen);
            this.txtGioDi.Text = cx.GIODI.ToShortTimeString();
            this.txtGioDen.Text = cx.GIODEN.ToShortTimeString();
            this.txtBenXeDi.Text = cx.BENXEDI;
            this.txtBenXeDen.Text = cx.BENXEDEN;
            this.cbbBenXeDen.Text = cx.BENXEDEN;
            this.cbbMaChuyen.SelectedIndex = 0;
            List<LichDiDTO> li = LichDiBUS.SelectLichDiByMaChuyen(machuyen);
            if (li.Count>0)
            {
                foreach (var item in li)
                {
                    this.cbbNgayDi.Items.Add(item.NGAYDI.ToShortDateString());
                }
                this.cbbNgayDi.SelectedIndex = 0;
            }
            cbbsohieu.DataSource = li;
            cbbsohieu.DisplayMember = "SOHIEU";
            cbbsohieu.ValueMember = "SOHIEU";
        }
        private void cbbBenXeDen_SelectedIndexChanged(object sender, EventArgs e)
        {
           String bden = this.cbbBenXeDen.Text;
           this.cbbMaChuyen.Items.Clear();
           
            List<ChuyenXeDTO> cx = ChuyenXeBUS.SelectChuyenXeByBenDen(bden);
            foreach (var item in cx)
            {
                this.cbbMaChuyen.Items.Add(item.MACHUYEN);
                LoadThongTinCX(item.MACHUYEN);
            }
            
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            String makhachhang = cbbMaKH.Text;
            DateTime ngaydi = Convert.ToDateTime(cbbNgayDi.Text);
            String machuyen = cbbMaChuyen.Text;
            String sohieu = cbbsohieu.Text;
            int soluong = Convert.ToInt32(txtSoLuong.Text);
            if (DatChoBUS.ThemDatCho(makhachhang, ngaydi, machuyen, sohieu, soluong))
            {
                MessageBox.Show("Thêm thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                LichDiBUS.SuaLichDi1(ngaydi, machuyen, sohieu);
                FormDatCho_Load(null, null);
            }
            else
            {
                MessageBox.Show("Thêm thất bại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            //String makhachhang = cbbMaKH.Text;
            int row = dataGridView1.CurrentRow.Index;
            String makhachhang = this.dataGridView1[0, row].Value.ToString();
            DateTime ngaydi = Convert.ToDateTime(dataGridView1[1, row].Value);
            String machuyen = dataGridView1[2, row].Value.ToString();
            String sohieu = dataGridView1[3, row].Value.ToString();
            int soluong = int.Parse(dataGridView1[4, row].Value.ToString());
            if (DatChoBUS.XoaDatCho(makhachhang, ngaydi, machuyen, sohieu, soluong))
            {
                MessageBox.Show("Xóa thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                LichDiBUS.SuaLichDi1(ngaydi, machuyen, sohieu);
                FormDatCho_Load(null, null);

            }
            else
            {
                MessageBox.Show("Xóa thất bại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            //String makhachhang = cbbMaKH.Text;
            //DateTime ngaydi = Convert.ToDateTime(cbbNgayDi.Text);
            //String machuyen = cbbMaChuyen.Text;
            //String sohieu = cbbsohieu.Text;
            //int soluong = Convert.ToInt32(txtSoLuong.Text);
            //if (DatChoBUS.SuaDatCho(makhachhang, ngaydi, machuyen, sohieu, soluong))
            //{
            //    MessageBox.Show("Sửa thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
            //else
            //{
            //    MessageBox.Show("Sửa thất bại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
